1
|
|
|
/* |
2
|
|
|
* Copyright (c) 2018-2019 Rafael da Silva Rocha. |
3
|
|
|
* |
4
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining |
5
|
|
|
* a copy of this software and associated documentation files (the |
6
|
|
|
* "Software"), to deal in the Software without restriction, including |
7
|
|
|
* without limitation the rights to use, copy, modify, merge, publish, |
8
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to |
9
|
|
|
* permit persons to whom the Software is furnished to do so, subject to |
10
|
|
|
* the following conditions: |
11
|
|
|
* |
12
|
|
|
* The above copyright notice and this permission notice shall be |
13
|
|
|
* included in all copies or substantial portions of the Software. |
14
|
|
|
* |
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
16
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
17
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
18
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
19
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
20
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
21
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @fileoverview Parse two's complement ints to and from byte buffers. |
27
|
|
|
* @see https://github.com/rochars/twos-complement-buffer |
28
|
|
|
*/ |
29
|
|
|
|
30
|
|
|
/** @module twos-complement-buffer */ |
31
|
|
|
|
32
|
|
|
import { UintBuffer } from 'uint-buffer'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* A class to parse two's complement signed integers |
36
|
|
|
* to and from byte buffers. |
37
|
|
|
* @extends UintBuffer |
38
|
|
|
*/ |
39
|
|
|
export class TwosComplementBuffer extends UintBuffer { |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param {number} bits The number of bits used by the integer. |
43
|
|
|
*/ |
44
|
|
|
constructor(bits) { |
45
|
|
|
super(bits); |
46
|
|
|
/** |
47
|
|
|
* @type {number} |
48
|
|
|
* @protected |
49
|
|
|
*/ |
50
|
|
|
this.max = Math.pow(2, this.bits) / 2 - 1; |
51
|
|
|
/** |
52
|
|
|
* @type {number} |
53
|
|
|
* @protected |
54
|
|
|
*/ |
55
|
|
|
this.min = -this.max - 1; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Write one two's complement signed integer to a byte buffer. |
60
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
61
|
|
|
* @param {number} num The number. |
62
|
|
|
* @param {number=} index The index being written in the byte buffer. |
63
|
|
|
* @return {number} The next index to write on the byte buffer. |
64
|
|
|
* @throws {TypeError} If num is NaN. |
65
|
|
|
* @throws {RangeError} On overflow. |
66
|
|
|
*/ |
67
|
|
|
pack(buffer, num, index=0) { |
68
|
|
|
return super.pack(buffer, num, index); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Read one two's complement signed integer from a byte buffer. |
73
|
|
|
* @param {!Uint8Array|!Array<number>} buffer An array of bytes. |
74
|
|
|
* @param {number=} index The index to read. |
75
|
|
|
* @return {number} |
76
|
|
|
* @throws {RangeError} On overflow. |
77
|
|
|
*/ |
78
|
|
|
unpack(buffer, index=0) { |
79
|
|
|
/** @type {number} */ |
80
|
|
|
let num = super.unpackUnsafe(buffer, index); |
81
|
|
|
num = this.sign_(num); |
82
|
|
|
this.overflow(num); |
83
|
|
|
return num; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Sign a number. |
88
|
|
|
* @param {number} num The number. |
89
|
|
|
* @return {number} |
90
|
|
|
* @private |
91
|
|
|
*/ |
92
|
|
|
sign_(num) { |
93
|
|
|
if (num > this.max) { |
94
|
|
|
num -= (this.max * 2) + 2; |
95
|
|
|
} |
96
|
|
|
return num; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|